package com.ruoyi.hr.controller.hr.changemanage.depchange;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.CompanysService;
import com.ruoyi.hr.service.DepartmentsService;
import com.ruoyi.hr.service.JobsService;
import com.ruoyi.hr.service.changemanage.depchange.DepChangeService;
import com.ruoyi.hr.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zeo
 * @explain 部门调动
 * @date 2020/3/12
 */
@Controller
@RequestMapping("/changeManage/DepChange")
public class DepChangeController extends BaseController {

    @Autowired
    DepChangeService depChangeService;

    @Autowired
    AuthorityService authorityService;

    @Autowired
    CompanysService companysService;

    @Autowired
    JobsService jobsService;

    @Autowired
    DepartmentsService departmentsService;

    private String prefix = "hr/changemanage/depchange";

    /**
     * 跳转分页页面
     */
    @GetMapping()
    public String DepChange() {
        return prefix + "/depChange";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(depChangeService.findDepChangeData(map, authority));
    }

    @GetMapping("/add")
    public String addDepChangeEmp() {
        return prefix + "/addDepChangeEmp";
    }

    @RequestMapping("/addDepChangeEmp")
    @ResponseBody
    public TableDataInfo addDepChangeEmp(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(depChangeService.addDepChangeEmp(map, authority));
    }

    /**
     * 执行添加员工存储过程
     */
    @PostMapping("/addInternshipChangeEmp")
    @ResponseBody
    public BaseResult addInternshipChangeEmp(String allEid) {
        return depChangeService.addInternshipChangeEmp(allEid);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Map map = depChangeService.getDepChangeEmpById(id);


        Long compId = Long.valueOf(String.valueOf(map.get("newCompid"))).longValue();
        Long depId = Long.valueOf(String.valueOf(map.get("newDepid"))).longValue();

        mmap.put("depChangeEmp", map);
        //公司
        mmap.put("companys", companysService.findAllCompanys(null));
        //部门
        mmap.put("departments", departmentsService.findDep((long) compId));
        //岗位
        mmap.put("jobs", jobsService.findJobs((long) depId));


        return prefix + "/editDepChangeEmp";
    }

    @GetMapping("/getNewWorkCity")
    public String getWorkCity() {
        return prefix + "/getNewWorkCity";
    }

    @GetMapping("/getNewReportTo")
    public String getReportTo(String type, ModelMap mmap) {
        mmap.put("type", type);
        return prefix + "/getNewReportTo";
    }


    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(depChangeService.updateDepChange(map));
    }


    /**
     * 数据确认,取消确认,执行
     */
    @PostMapping("/procedure")
    @ResponseBody
    public BaseResult procedure(String allId, String procedure) {
        return depChangeService.procedure(allId, procedure);
    }

    @GetMapping("/history")
    public String history() {
        return prefix + "/history";
    }

    @RequestMapping("/historyList")
    @ResponseBody
    public TableDataInfo historyList(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(depChangeService.historyList(map, authority));
    }

}
